package com.gsm.service.impl;

import com.gsm.dao.SysUserDao;
import com.gsm.dao.SysUserRoleDao;
import com.gsm.dto.SysUserDto;
import com.gsm.entity.RespCode;
import com.gsm.entity.Result;
import com.gsm.po.SysUser;
import com.gsm.po.SysUserRole;
import com.gsm.service.SysUserService;
import com.gsm.utils.Base64Utils;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台管理人员账户(SysUser)表服务实现类
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;


    @Override
    public Result insertSysUser(SysUserDto dto) {
        long userId = idUtils.nextId();
        SysUser sysUser = sysUserDao.selectSysUserByIphoneNo(dto.getIphoneNo());
        if (sysUser != null) {
            return Result.error(RespCode.REG_REGISTERED);
        }

        //查询最大排序
        int maxShowOrder = sysUserDao.selectMaxShowOrder();
        SysUser user = SysUser.builder()
                .userId(userId)
                .userName(dto.getUserName())
                .iphoneNo(dto.getIphoneNo())
                .po(Base64Utils.base64Encode(dto.getPo()))
                .pc(passwordEncoder.encode(dto.getPc()))
                .headUrl("http://wyxjava.oss-cn-shanghai.aliyuncs.com/1258004746996944896.jpg")
                .status(1)
                .showOrder(maxShowOrder + 1)
                .createTime(new Date())
                .build();
        //插入用户信息
        sysUserDao.insertSysUser(user);

        //给用户分配角色
        List<SysUserRole> lists = new ArrayList();
        List<Long> sysRoles = dto.getSysRoles();
        sysRoles.forEach(i -> {
            SysUserRole sysUserRole = SysUserRole.builder()
                    .userRoleId(idUtils.nextId())
                    .userId(userId)
                    .roleId(i)
                    .build();
            lists.add(sysUserRole);
        });
        sysUserRoleDao.insertSysUserRoles(lists);
        return Result.success();
    }

    /**
     * 根据 主键 userId 删除
     *
     * @param userId 主键
     * @return
     */
    @Override
    public Result deleteSysUserByUserId(Long userId) {
        sysUserDao.deleteSysUserByUserId(userId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param dto 实例对象
     * @return
     */
    @Override
    public Result updateSysUserByUserId(SysUserDto dto) {
        SysUser user = sysUserDao.selectSysUserByIphoneNo(dto.getIphoneNo());
        if (!user.getUserId().equals(dto.getUserId())) {
            return Result.error(RespCode.REG_REGISTERED);
        }
        String po = "";
        String pc = "";
        if (dto.getPo() == null || "".equals(dto.getPo())) {
            po = user.getPo();
        } else {
            po = Base64Utils.base64Encode(dto.getPo());
        }
        if (dto.getPc() == null || "".equals(dto.getPc())) {
            pc = user.getPc();
        } else {
            pc = passwordEncoder.encode(dto.getPc());
        }
        //更新用户信息
        SysUser sysUser = SysUser.builder()
                .userId(dto.getUserId())
                .userName(dto.getUserName())
                .iphoneNo(dto.getIphoneNo())
                .po(po)
                .pc(pc)
                .build();
        sysUserDao.updateSysUserByUserId(sysUser);

        //删除用户角色
        sysUserRoleDao.deleteSysUserRoleByUserId(dto.getUserId());

        //给用户分配角色
        List<SysUserRole> lists = new ArrayList();
        List<Long> sysRoles = dto.getSysRoles();
        sysRoles.forEach(i -> {
            SysUserRole sysUserRole = SysUserRole.builder()
                    .userRoleId(idUtils.nextId())
                    .userId(dto.getUserId())
                    .roleId(i)
                    .build();
            lists.add(sysUserRole);
        });
        sysUserRoleDao.insertSysUserRoles(lists);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param userId 主键
     * @return
     */
    @Override
    public Result selectSysUserByUserId(Long userId) {
        SysUser sysUser = sysUserDao.selectSysUserByUserId(userId);
        Result result = Result.success(sysUser);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param sysUser 实例对象
     * @return
     */
    @Override
    public Result selectSysUserList(SysUser sysUser) {
        List<SysUser> list = sysUserDao.selectSysUserListAndRoles(sysUser);
        return Result.success(list);
    }

    @Override
    public Result updateSysUserShowOrder(List<Long> userIds) {
        sysUserDao.updateSysUserShowOrderByUserIds(userIds);
        return Result.success();
    }
}